#echo "Loading bash_profile"
# set -ex
[[ -s "$HOME/.profile" ]] && source "$HOME/.profile" # Load the default .profile

if [ -n "$(uname -a | grep -i darwin)" ] ; then
    if [ -f `brew --prefix`/etc/bash_completion ]; then
       . `brew --prefix`/etc/bash_completion
    fi
fi
# [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
# Git branch in prompt.
parse_git_branch() {

    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'

}

#export PS1="\u@\h:[\w]\[\033[32m\]\$(parse_git_branch)\[\033[00m\]$ "
export PS1="\u:[\w]\[\033[32m\]\$(parse_git_branch)\[\033[00m\]$ "

# git-completion
git_completion_bash=/Library/Developer/CommandLineTools/usr/share/git-core/git-completion.bash
[[ -f $git_completion_bash ]] && source $git_completion_bash
#### Command history related configurations######
# erase duplicates from command histories
export HISTCONTROL=erasedups
# ignore some commands from history
export HISTIGNORE="pwd:ls:ls -ltr:"
# The resulting history line is loaded into the Readline editing buffer, 
# allowing further modification.
shopt -s histverify
# always append histories
shopt -s histappend
# format history with time 
export HISTTIMEFORMAT='%F %T '

# need to link to autossh in order for this to work
# ln -s /usr/bin/autossh ~/bin/ssh
# and add ServerAlivedInterval and ServerAliveCountMax to ~/.ssh/config
export AUTOSSH_PORT=0

export INFORMIXDIR='/usr/local/clientsdk.3.5'
export INFORMIXSQLHOSTS="$INFORMIXDIR/etc/sqlhosts.ifx"
export INFORMIXSERVER='ifx_local'
export DYLD_LIBRARY_PATH="$INFORMIXDIR/lib:$INFORMIXDIR/lib/esql"

# golang setting
if test -d ~/dev/go/; then
    export GOPATH=~/dev/go/
    export GOROOT=/usr/local/go/
    export PATH=${GOPATH//://bin:}/bin:$GOROOT/bin/:$PATH
fi

# mactex path
if test -d /Library/TeX/textin; then
    export PATH=/Library/TeX/textbin/:$PATH
fi

# use bclab proxy
#export http_proxy=http://bclab.sl.cloud9.ibm.com:9200
#export https_proxy=http://bclab.sl.cloud9.ibm.com:9200

[[ -f ~/.bashrc ]] && source ~/.bashrc
[[ -f ~/.bashrc_local ]] && source ~/.bashrc_local
[[ -f "$HOME/.bash_aliases" ]] && source "$HOME/.bash_aliases" # Load aliases
export LANG="en_US.UTF-8"


function fs()
{
    KEY=$1
    FILEP=$2
    [ -z $2 ] && FILEP='*.*'
    #grep -I -i "$KEY" .  -r --include "$FILEP" | awk -F ':' '{if(a[$1]++==0) print $0}'
    grep -n -I -i "$KEY" .  -r --include "$FILEP" 
}
function fsf()
{
    KEY=$1
    FILEP=$2
    [ -z $2 ] && FILEP='*.*'
    grep -i -I "$KEY" . -r --include "$FILEP" | awk -F ':' '{if(a[$1]++==0) print $1}'
    find . -iname "$KEY"
}
function vif()
{
    KEY=$1
    FILEP=$2
    [ -z $2 ] && FILEP='*.*'
    vim $(grep -I -i "$KEY" .  -r --include "$FILEP" | awk -F ':' '{if(a[$1]++==0) print $1}') +/"$KEY"
}
function title()
{
    TITLE=$1
    [ -z $1 ] && TITLE=`pwd | awk -F '/' '{print $NF}'` 
    echo -n -e "\033];$TITLE\007"
}
function git_add()
{
    git add `git status | awk '/modified/ && NF==2 { print $2 }'`
}

function git_add_a()
{
    git add `git status | awk '/modified/ && NF==2 { print $2 }'`
    git commit --amend
}

function git_add_m()
{
    echo "adding merged files"
    git add `git status -s | grep '^UU ' | grep -v 'repo-deps' | sed 's/^UU //'`
}
function git_add_push()
{
	git_add && git commit --amend && gerrit_push
}
function git_rm()
{
	git status -s | sed -n -e 's/^ D // p' | xargs -I {} git rm {}
}

function git_fix_pc()
{
#now=`date "+DATE: %Y%m%dT%H%M%S" | awk '{print $2}'`
#git status -s | awk '/^[AM]/ { print $2}'| grep -E -v '\.pdb$|\.dll$|\.exe$' > .git_fix_temp
#git status -s | awk '/^[R]/ { print $4}' | grep -E -v '\.pdb$|\.dll$|\.exe$' >> .git_fix_temp
git show | awk '/^diff/' | awk 'match($0,/b\//) {print substr($0,RSTART+2, length($0))}' | grep -E -v '\.pdb$|\.dll$|\.exe$'> .git_fix_temp
cat .git_fix_temp | grep '\.cs' | xargs -I {} chmod a-x {}
cat .git_fix_temp | xargs -I {} dos2unix {}
cat .git_fix_temp | xargs -I {} sed -E -i '' 's/[     ]+$//' {}
rm .git_fix_temp
}
function git_ls_pc()
{
git show | awk '/^diff/' | awk 'match($0,/b\//) {print substr($0,RSTART+2, length($0))}' | grep -E -v '\.pdb$|\.dll$|\.exe$' 
}

function git_ls_new()
{
git status -s | awk '/^\?\? / { print substr($0,RSTART+4,length($0)) }'
}

function git_ls_m()
{
git status -s | awk '/^ M/ { print substr($0,RSTART+4,length($0)) }' | grep -v 'repo-deps'
}

function git_ue_m()
{
 git status -s | sed -n 's/^[ ]M[ ]// p' | grep -v '\(dll\|exe\|pdb\)$' | xargs -I {} dos2unix {} 
}
function git_ue_new()
{
  git status -s | sed -n 's/^?? //p' | xargs -I {} dos2unix {}
}
function git_ue_mm()
{
 git status -s | sed -n 's/^M  // p' | grep -v '\(dll\|exe\|pdb\)$' | xargs -I {} dos2unix {} 
}

function git_p()
{
 git status -s | sed -n 's/^ [AM][AM]* // p' | grep -v '\(dll\|exe\|pdb\)$'
}

function git_trim_m()
{
	git status -s | sed -n 's/^[ ]M[ ]// p' | grep -v '\(dll\|exe\|pdb\)$' | xargs -I {} sed -E -i -e 's/[\t ]+$//' {}
}

function git_trim()
{
   sed -E -i -e 's/[ ]+$//' $1
}

function gerrit_push()
{
	git pull --rebase;gerrit push
}


# function md()
# {
#     /Applications/MacDown.app/Contents/MacOS/MacDown $@ &
# }
# 
function stopvm()
{
    vboxmanage list vms | grep $1 | awk '{print $2}' | xargs -I {} vboxmanage controlvm {} savestate 
}
function startvm()
{
    vboxmanage list vms | grep $1 | awk '{print $2}' | xargs -I {} vboxmanage startvm {} --type headless
}
